Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use std::make_{unique,shared} instead of raw new #3535

Merged

Conversation

ManManson
Copy link
Member

@ManManson ManManson commented Nov 24, 2023

This is one of the first steps to improve memory management in the Warzone 2100 project.

There are many places which use raw new/delete allocations along with extensive usage of raw pointers.

This changeset aims to replace raw allocations with std::make_unique and std::make_shared instead of the std::unique_ptr<...>(new ...) and std::shared_ptr<...>(new ...) constructs, respectively.

Since the project-wide C++ standard defaults to C++14, we should be able to use std::make_{unique,shared} without problem.

Among others, std::make_{unique,shared} has the following advantages over using raw new/delete with std::{unique,shared}_ptr constructors:

  • Provides terser syntax without an additional repetition of the constructed type.
  • Provides stronger exception-safety guarantees.

No impact on any of the existing functionality is expected.

This is one of the first steps to improve
memory management in the Warzone 2100 project.

There are many places which use raw `new/delete`
allocations along with extensive usage of
raw pointers.

This changeset aims to replace raw allocations
with `std::make_unique` and `std::make_shared`
instead of the `std::unique_ptr<...>(new ...)`
and `std::shared_ptr<...>(new ...)` constructs,
respectively.

Since the project-wide C++ standard defaults
to C++14, we should be able to use
`std::make_{unique}` without problem.

Among others, `std::make_{unique,shared}` has
the following advantages over using raw
`new/delete` with `std::{unique,shared}_ptr`
constructors:

 * Provides terser syntax without an additional
   repetition of the constructed type.
 * Provides stronger exception-safety guarantees.

No impact on any of the existing functionality
is expected.

Signed-off-by: Pavel Solodovnikov <[email protected]>
@ManManson ManManson force-pushed the make_shared_unique_instead_of_raw_alloc branch from 045ce61 to 9bec7d9 Compare November 24, 2023 09:51
`wzmaplib` is used directly by other projects, so we
still need to be careful not to upgrade the C++
standard version automatically: this part should
be self-contained.

Signed-off-by: Pavel Solodovnikov <[email protected]>
src/map.cpp Fixed Show fixed Hide fixed
@ManManson ManManson force-pushed the make_shared_unique_instead_of_raw_alloc branch from 6d1c739 to 8ae688b Compare November 27, 2023 20:18
@past-due past-due added this to the 4.4.2 milestone Nov 28, 2023
@past-due past-due merged commit a1064d9 into Warzone2100:master Nov 28, 2023
35 checks passed
@past-due
Copy link
Member

Merged. Thanks @ManManson !

(Btw, if you haven't joined the Discord yet, we have some dev channels & discussions in there as well. Join link is here: https://wz2100.net/webchat/ )

@ManManson ManManson deleted the make_shared_unique_instead_of_raw_alloc branch November 28, 2023 19:47
@ManManson
Copy link
Member Author

OK, thanks @past-due !
Will definitely join the dev community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants